/**
 * 
 */
package com.effectiv.gooruda.rest.api.service;

import java.util.List;

import jodd.cache.LFUCache;
import jodd.mail.CommonEmail;
import jodd.mail.Pop3Server;
import jodd.mail.Pop3SslServer;
import jodd.mail.ReceiveMailSession;
import jodd.mail.ReceivedEmail;
import jodd.mail.SendMailSession;
import jodd.mail.SmtpServer;
import jodd.mail.SmtpSslServer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;

import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import com.effectiv.gooruda.rest.api.domain.Email;
import com.effectiv.gooruda.rest.api.domain.EmailServerConfiguration;
import com.effectiv.gooruda.rest.api.exception.DuplicateDataException;
import com.effectiv.gooruda.rest.api.exception.NoDataFoundException;
import com.effectiv.gooruda.rest.api.operation.EmailOperations;
import com.effectiv.gooruda.security.rest.RESTSecurityUtil;

/**
 * @author Dhrubo
 * 
 */
@Component
public class EmailServiceImpl implements EmailOperations {

	@Autowired
	private MongoTemplate template;
	
	@Autowired
	private EmailHandlerTemplate emailTemplate;

	private final String EMAIL_CONFIG_COLLECTION_BASE = "t_email_config_";

	private LFUCache<String, SmtpServer> serverCache = new LFUCache<String, SmtpServer>(
			50, 600000);
	
	private LFUCache<String, Pop3Server> serverCachePop3 = new LFUCache<String, Pop3Server>(
			50, 600000);

	public EmailServerConfiguration findOneConfigurationByName(String name) {
		Query query = new Query();
		query.addCriteria(Criteria.where("name").is(name));

		return template.findOne(query, EmailServerConfiguration.class,
				EMAIL_CONFIG_COLLECTION_BASE + RESTSecurityUtil.getAppId());
	}

	
	@Override
	public void send(String configName, Email email, List<MultipartFile> attachments, List<MultipartFile> inlines) {
		
		SmtpServer server = serverCache.get(configName);

		if (server == null) {
			EmailServerConfiguration config = findOneConfigurationByName(configName);
			
			if(config == null) {
				throw new NoDataFoundException("Email configuration not found with config name - " + configName);
			}
			
			if (config.isSSL()) {
				server = new SmtpSslServer(config.getHost(), config.getPort(),
						config.getUserName(), config.getPassword());
			} else {
				server = new SmtpServer(config.getHost(), config.getPort(),
						config.getUserName(), config.getPassword());
			}
			serverCache.put(configName, server);
		}	

		//emailTemplate.send(email, server, file);
	}
	
	/*
	@Override
	
	public ReceivedEmail[] receive(String configName) {
		Pop3Server server = serverCachePop3.get(configName);

		if (server == null) {
			EmailServerConfiguration config = findOneConfigurationByName(configName);
			
			if(config == null) {
				throw new NoDataFoundException("Email configuration not found with config name - " + configName);
			}
			
			if (config.isSSL()) {
				server = new Pop3SslServer(config.getHost(), config.getPort(),
						config.getUserName(), config.getPassword());
			} else {
				server = new Pop3Server(config.getHost(), config.getPort(),
						config.getUserName(), config.getPassword());
			}
			serverCachePop3.put(configName, server);
		}
		
		ReceiveMailSession session = server.createSession();
		session.open();
		ReceivedEmail[] emails = session.receiveEmail(false);
		session.close();
		return emails;
	}
	*/
}
